<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"
	xmlns:cs="http://myfaces.apress.com/custom"
	template="/WEB-INF/layouts/standard.xhtml" xmlns:a4j="http://richfaces.org/a4j" xmlns:rich="http://richfaces.org/rich"
	xmlns:bf="http://www.bssmanager.com/jsf" xmlns:c="http://java.sun.com/jstl/core">

	<ui:define name="formheader">
		<h2>Payments</h2>
	</ui:define>
	
	<ui:define name="content">
	
		<h:form name="paymentForm" prependId="false">		
				<table style="table-layout: fixed;" align="left">					
					<tr class="norender">
						<td colspan="3" style="background-color: #FFBF80; font-weight: bolder; font-size: 10pt;" align="left">Select
						Patient</td>
						<td colspan="3" style="background-color: #FFBF80; font-weight: bolder; font-size: 10pt;" align="left">Select
						Date</td>
					</tr>
					<tr class="norender">
						<td colspan="3" align="left">
							<bf:admissionSelector controller="#{paymentController}" selectMethod="selectAdmission" reRender="selectedPatient,tabPayments,dtRcpts"/> 
						</td>
						<td colspan="3" align="left">
							<rich:calendar inputSize="14" enableManualInput="false" value="#{paymentController.date}" datePattern="dd MMM yyyy HH:mm"/>							
						</td>
					</tr>
					<tr class="norender">
						<td colspan="6" style="background-color: #FFBF80; font-weight: bolder; font-size: 10pt;" align="center">
						Patient Details</td>
					</tr>								
			
			<tr>
				<td colspan="6">			
					<h:panelGroup id="selectedPatient" style="display:#{paymentController.admission.id le 0 ? 'none':'block' }">
						<h:outputText
							value="#{paymentController.admission.dateOfExit == null ? '': 'Patient has Checked Out !'} #{paymentController.admission.dateOfExit == null ? '': messages['payment.error.not-allowed']}"
							style="font-weight:bold; color:red" />
						<h:panelGrid columns="2">
							<h:outputLabel value="Name: " />
							<h:outputText value="#{paymentController.admission.patient.name}" />
							<h:outputLabel value="Checked In: " />
							<h:outputText value="#{paymentController.admission.dateOfAdmission}">
								<f:convertDateTime pattern="dd MMM yyyy HH:MM" />
							</h:outputText>							
							<h:outputLabel value="Utensil Deposit: " />
							<h:outputText value="#{paymentController.admission.materialSet.deposit}" />
						</h:panelGrid>				
					</h:panelGroup>
				</td>
			</tr>
			
			<tr>
				<td colspan="6" style="background-color: #FFBF80; font-weight: bolder; font-size: 10pt;" align="center">
					Receipt Details
				</td>
			</tr>					
			<tr class="norender">
				<td colspan="6">
					<h:panelGroup id="receiptInfo">
						<c:choose>
							<c:when test="#{ paymentController.selectedReceipt == null }">
								<div><h:outputText value="New Receipt will be created for new deposits/payments!"/></div>
								<div><h:outputText value="Choose an existing receipt from the list below" style="color:red"/></div>
							</c:when>
							<c:otherwise>
								<h:panelGrid columns="2">
									<h:outputLabel value="Receipt ID: " />
									<h:outputText value="#{paymentController.selectedReceipt.id}"/>													
									<h:outputLabel value="Remarks: " />						
									<h:outputText value="#{paymentController.selectedReceipt.remarks}"/>
									<h:outputLabel value="Created Date: " />
									<h:outputText value="#{paymentController.selectedReceipt.date}">
										<f:convertDateTime pattern="dd MMM yyyy HH:mm" timeZone="GMT+05:30"/>
									</h:outputText>	
								</h:panelGrid>
							</c:otherwise>					
						</c:choose>
					</h:panelGroup>
				</td>
			</tr>
			<tr style="background-color: #FFBF80; height:25px" />
			<tr>
				<td colspan="6">			
					<rich:tabPanel  id="tabPayments" style="display:#{paymentController.admission.id le 0 ? 'none':'block' }; width:90%;">
							<rich:tab rendered="#{paymentController.accessFinder['Room and Dormitory Payment'] and false}" label="Room/Dormitory" switchType="client"  >
								<table>
									<tr>
										<td valign="top">											
											<rich:dataTable value="#{paymentController.roomBookings}" id="dtRB1" var="aBean">
												<f:facet name="header">
													<h:outputText value="Room Bookings"/>
												</f:facet>
												<rich:column>
													<f:facet name="header">Pay</f:facet>
													<h:selectBooleanCheckbox value="#{aBean.selected}" disabled="#{not empty paymentController.admission.dateOfExit or not empty aBean.checkOutDate}">													
														<a4j:support action="#{paymentController.selectRoomForPayment}" event="onclick" reRender="dtRB1"/>
													</h:selectBooleanCheckbox>													
												</rich:column>
												<rich:column>
													<f:facet name="header">Room Number</f:facet>
													<h:outputText value="#{aBean.room.building.name}-#{aBean.room.roomNumber} "/>
												</rich:column>
												<rich:column>
													<f:facet name="header">CheckIn Date</f:facet>
													<h:outputText value="#{aBean.checkInDate}">
														<f:convertDateTime pattern="dd MMM yyyy" />
													</h:outputText>
												</rich:column>								
												<rich:column>
													<f:facet name="header">CheckOut Date</f:facet>
													<h:outputText value="#{aBean.checkOutDate}" rendered="#{not empty aBean.checkOutDate}">
														<f:convertDateTime pattern="dd MMM yyyy" />
													</h:outputText>
													<c:if test="#{empty aBean.checkOutDate}">
														<h:outputText value="N/A"/>
													</c:if>
												</rich:column>
												<rich:column>
													<f:facet name="header">Rate</f:facet>
													<h:outputText value="#{aBean.rate} "/>
												</rich:column>
												<rich:column>
													<f:facet name="header">Paid</f:facet>
													<h:outputText value="#{aBean.totalPayment == null ? 0 : aBean.totalPayment}"/>
												</rich:column>
												<rich:column>
													<f:facet name="header">Pending</f:facet>
													<h:outputText value="#{aBean.pendingPayment == null ? 0 : aBean.pendingPayment}"/>
												</rich:column>
												<rich:column>
													<f:facet name="header">Payment</f:facet>
													<h:inputText size="4" disabled="#{not empty aBean.checkOutDate or not aBean.selected}" value="#{aBean.payment}" style="background-color: #{ aBean.selected ? 'lime':'gray'};"/>													
												</rich:column>												
												
												<rich:subTable value="#{aBean.payments}" var="payment">													
													<rich:column colspan="3">
														<f:facet name="header">
															<h:outputLabel style="font-weight:bold" value="Payments for Room #{aBean.room.building.name}-#{aBean.room.roomNumber} "/>
														</f:facet>			
													</rich:column>
													<rich:column>
														<f:facet name="header">Payment Date</f:facet>
														<h:outputText value="#{payment.receipt.date}">
															<f:convertDateTime pattern="dd MMM yyyy HH:mm" timeZone="GMT+05:30" />
														</h:outputText>
													</rich:column>		
													<rich:column>
														<f:facet name="header">Start Date</f:facet>
														<h:outputText value="#{payment.dateStart}">
															<f:convertDateTime pattern="dd MMM yy" />
														</h:outputText>
													</rich:column>									
													<rich:column>
														<f:facet name="header">End Date</f:facet>
														<h:outputText value="#{payment.dateEnd}">
															<f:convertDateTime pattern="dd MMM yy" />
														</h:outputText>
													</rich:column>
													<rich:column>
														<f:facet name="header">Receipt</f:facet>
														<a href="#" onclick="launchChildWindow('${request.contextPath}/pages/receipt.jsf?id=#{payment.receipt.id}')">#{payment.receipt.id}</a>
													</rich:column>						
													<rich:column>
														<f:facet name="header">Amount</f:facet>
														<h:outputText value="#{payment.amount} "/>
													</rich:column>																																																										
													<c:if test="#{empty aBean.payments}">
														<f:facet name="footer">
															<h:outputText value="No Payments made..." />
														</f:facet>
													</c:if>
												</rich:subTable>
																																																	
												<c:if test="#{paymentController.roomBookings.rowCount le 0}">
													<f:facet name="footer">
														<h:outputText value="No Bookings..." />
													</f:facet>
												</c:if>	
											</rich:dataTable>																					
										</td>																			
									</tr>
									<tr>
										<td colspan="2">&nbsp;</td>
									</tr>
									<tr>
										<td valign="top">
											<rich:dataTable value="#{paymentController.bedBookings}" var="aBean" id="dtBB">
												<f:facet name="header">
													<h:outputText value="Dormitory Bookings"/>
												</f:facet>
												<rich:column>
													<f:facet name="header">Pay</f:facet>
													<h:selectBooleanCheckbox value="#{aBean.selected}" disabled="#{not empty paymentController.admission.dateOfExit or not empty aBean.checkOutDate}">													
														<a4j:support action="#{paymentController.selectBedForPayment}" event="onclick" reRender="dtBB"/>
													</h:selectBooleanCheckbox>													
												</rich:column>
												<rich:column>
													<f:facet name="header">Bed Number</f:facet>
													<h:outputText value="#{aBean.bed.room.building.name}(#{aBean.bed.room.roomNumber})-#{aBean.bed.bedNumber} "/>
												</rich:column>
												<rich:column>
													<f:facet name="header">CheckIn Date</f:facet>
													<h:outputText value="#{aBean.checkInDate}">
														<f:convertDateTime pattern="dd MMM yyyy" />
													</h:outputText>
												</rich:column>									
												<rich:column>
													<f:facet name="header">CheckOut Date</f:facet>
													<h:outputText value="#{aBean.checkOutDate}" rendered="#{not empty aBean.checkOutDate}">
														<f:convertDateTime pattern="dd MMM yyyy" />
													</h:outputText>
													<c:if test="#{empty aBean.checkOutDate}">
														<h:outputText value="N/A"/>
													</c:if>
												</rich:column>							
												<rich:column>
													<f:facet name="header">Rate</f:facet>
													<h:outputText value="#{aBean.rate} "/>
												</rich:column>
												<rich:column>
													<f:facet name="header">Paid</f:facet>
													<h:outputText value="#{aBean.totalPayment == null ? 0 : aBean.totalPayment}"/>
												</rich:column>
												<rich:column>
													<f:facet name="header">Pending</f:facet>
													<h:outputText value="#{aBean.pendingPayment == null ? 0 : aBean.pendingPayment}"/>
												</rich:column>																	
												<rich:column>
													<f:facet name="header">Payment</f:facet>
													<h:inputText size="4" disabled="#{not empty aBean.checkOutDate or not aBean.selected}" value="#{aBean.payment}" style="background-color: #{ aBean.selected ? 'lime':'gray'};"/>													
												</rich:column>
												
												<rich:subTable value="#{aBean.payments}" var="payment">													
													<rich:column colspan="3">
														<f:facet name="header">
															<h:outputLabel style="font-weight:bold" value="Payments for Room #{aBean.bed.room.building.name}-#{aBean.bed.room.roomNumber} "/>
														</f:facet>			
													</rich:column>
													<rich:column>
														<f:facet name="header">Payment Date</f:facet>
														<h:outputText value="#{payment.receipt.date}">
															<f:convertDateTime pattern="dd MMM yyyy HH:mm" timeZone="GMT+05:30" />
														</h:outputText>
													</rich:column>											
													<rich:column>
														<f:facet name="header">Start Date</f:facet>
														<h:outputText value="#{payment.dateStart}">
															<f:convertDateTime pattern="dd MMM yyyy" />
														</h:outputText>
													</rich:column>									
													<rich:column>
														<f:facet name="header">End Date</f:facet>
														<h:outputText value="#{payment.dateEnd}">
															<f:convertDateTime pattern="dd MMM yyyy" />
														</h:outputText>
													</rich:column>
													<rich:column>
														<f:facet name="header">Receipt</f:facet>
														<a href="#" onclick="launchChildWindow('${request.contextPath}/pages/receipt.jsf?id=#{payment.receipt.id}')">#{payment.receipt.id}</a>
													</rich:column>						
													<rich:column>
														<f:facet name="header">Amount</f:facet>
														<h:outputText value="#{payment.amount} "/>
													</rich:column>																																																										
													<c:if test="#{empty aBean.payments}">
														<f:facet name="footer">
															<h:outputText value="No Payments made..." />
														</f:facet>
													</c:if>
												</rich:subTable>
																																							
												<c:if test="#{paymentController.bedBookings.rowCount le 0 }">
													<f:facet name="footer">
														<h:outputText value="No Bookings..." />
													</f:facet>
												</c:if>	
											</rich:dataTable>
										</td>
									</tr>
									<tr>
										<td colspan="2">
											<a4j:commandButton value="Make Payment" 
											action="#{paymentController.submit}" reRender="dtRB1,dtBB,dtRcpts"
													styleClass="noprint" />
										</td>
									</tr>							
								</table>
							</rich:tab>
							<rich:tab rendered="#{paymentController.accessFinder['Donation']}" label="Donation" switchType="client" >
								<table>
									<tr>
										<td valign="top">
											<h:panelGrid columns="2" styleClass="noprint">
												<h:outputLabel value="Amount: "/>
												<h:inputText id="txtDonPay" onkeyup="validateInteger(this,true,'Only numbers allowed !');" size="4" value="#{paymentController.donationAmount}"  />
											</h:panelGrid>
											<a4j:commandButton onclick="if(document.getElementById('txtDonPay').value == 0) { alert('Amount is not valid !'); return false;}"
																value="Donate" action="#{paymentController.donate}" reRender="dtDonations,dtRcpts"/>									
										</td>
										<td valign="top">
											<rich:dataTable value="#{paymentController.donations}" var="payment" id="dtDonations">
												<rich:column>
													<f:facet name="header">Payment Date</f:facet>
													<h:outputText value="#{payment.receipt.date}">
														<f:convertDateTime pattern="dd MMM yyyy HH:mm" timeZone="GMT+05:30"/>
													</h:outputText>
												</rich:column>									
												<rich:column>
													<f:facet name="header">Receipt</f:facet>
													<a href="#" onclick="launchChildWindow('${request.contextPath}/pages/receipt.jsf?id=#{payment.receipt.id}')">#{payment.receipt.id}</a>
												</rich:column>
												<rich:column>
													<f:facet name="header">Amount</f:facet>
													<h:outputText value="#{payment.amount} " />
												</rich:column>																						
											</rich:dataTable>										
										</td>
									</tr>
								</table>
							</rich:tab>
							<rich:tab id="tabSecDep" rendered="#{paymentController.accessFinder['Security Deposit']}" label="Security Deposit" switchType="client" >
								<table>
									<tr>
										<td valign="top">
											<h:panelGrid columns="2" styleClass="noprint" id="secDepGrid"
												rendered="#{paymentController.visibilityFinder['Security Deposit,deposit'] or paymentController.visibilityFinder['Security Deposit,refund']}">
												<h:outputLabel value="Amount: "/>
												<h:inputText 	 
																onkeyup="validateInteger(this,true,'Only numbers allowed !');" 
																size="4" id="txtSecDep"
																value="#{paymentController.securityDepositAmount}"
																title="Amount must be a number greater than 0">
												</h:inputText>
											
											<a4j:commandButton onclick="if(document.getElementById('txtSecDep').value == 0) { alert('Amount is not valid !'); return false;} " 
																value="Deposit"
																action="#{paymentController.securityDeposit}" 
																reRender="dtSecurityDeposits,dtRcpts,secDepGrid"
																rendered="#{paymentController.visibilityFinder['Security Deposit,deposit']}"
																oncomplete="if(#{paymentController.depositComplete}) { alert('Security Deposit and Utensil Deposit is complete for #{paymentController.admission.patient.name}!\nGoing to ID Generation Page...'); document.location.href='generateId.jsf?id=#{paymentController.admission.id}'}"
																/>
											<a4j:commandButton onclick="if(document.getElementById('txtSecDep').value == 0) { alert('Amount is not valid !'); return false;} " 
																value="Refund" 
																action="#{paymentController.securityDepositRefund}" 
																reRender="dtSecurityDeposits,dtRcpts,secDepGrid"
																rendered="#{paymentController.visibilityFinder['Security Deposit,refund']}"
																oncomplete="if(#{paymentController.refundComplete}) { alert('Refund and Checkout for #{paymentController.admission.patient.name} is complete !\nGoing to Checkout page...'); document.location.href='exit.jsf'}"
																
																/>	
											</h:panelGrid>																												
										</td>
										<td valign="top">
											<rich:dataTable value="#{paymentController.securityDeposits}" var="payment" id="dtSecurityDeposits">
												<rich:column>
													<f:facet name="header">Payment Date</f:facet>
													<h:outputText value="#{payment.receipt.date}">
														<f:convertDateTime pattern="dd MMM yyyy HH:mm" timeZone="GMT+05:30"/>
													</h:outputText>
												</rich:column>									
												<rich:column>
													<f:facet name="header">Receipt</f:facet>
													<a href="#" onclick="launchChildWindow('${request.contextPath}/pages/receipt.jsf?id=#{payment.receipt.id}')">#{payment.receipt.id}</a>
												</rich:column>
												<rich:column>
													<f:facet name="header">Amount</f:facet>
													<h:outputText value="#{payment.amount} " />
												</rich:column>
												<rich:column>
													<f:facet name="header">Is Refund</f:facet>
													#{payment.refund ? 'Yes':''}													
												</rich:column>																								
											</rich:dataTable>										
										</td>
									</tr>
								</table>
							</rich:tab>
							<rich:tab rendered="#{paymentController.accessFinder['Utensil Deposit']}"  label="Utensil Deposit" switchType="client" style="width:100px;">
								<table>
									<tr>
										<td valign="top">
											<h:panelGrid columns="2" styleClass="noprint" id="utDepGrid"
											rendered="#{paymentController.visibilityFinder['Utensil Deposit,deposit'] or paymentController.visibilityFinder['Utensil Deposit,refund']}">
												<h:outputLabel value="Amount: "/>
												<h:inputText id="txtUteDepPay" 
															onkeyup="validateInteger(this,true,'Only numbers allowed !');" size="4" value="#{paymentController.utensilDepositAmount}"/>
																						
											<a4j:commandButton onclick="if(document.getElementById('txtUteDepPay').value == 0) { alert('Amount is not valid !'); return false;} " 
																value="Deposit" action="#{paymentController.utensilDeposit}" 
																reRender="dtUtensilDeposits,dtRcpts,utDepGrid"
																rendered="#{paymentController.visibilityFinder['Utensil Deposit,deposit']}"
																oncomplete="if(#{paymentController.depositComplete}) { alert('Security Deposit and Utensil Deposit is complete for #{paymentController.admission.patient.name}!\nGoing to ID Generation Page...'); document.location.href='generateId.jsf?id=#{paymentController.admission.id}'}"
																/>
											<a4j:commandButton onclick="if(document.getElementById('txtUteDepPay').value == 0) { alert('Amount is not valid !'); return false;} " 
																value="Refund" action="#{paymentController.utensilDepositRefund}" 
																reRender="dtUtensilDeposits,dtRcpts,utDepGrid"
																rendered="#{paymentController.visibilityFinder['Utensil Deposit,refund']}"
																oncomplete="if(#{paymentController.refundComplete}) { alert('Refund and Checkout for #{paymentController.admission.patient.name} is complete !\nGoing to Checkout page...'); document.location.href='exit.jsf'}"/>
											</h:panelGrid>
										</td>
										<td valign="top">
											<rich:dataTable value="#{paymentController.utensilDeposits}" var="payment" id="dtUtensilDeposits">
												<rich:column>
													<f:facet name="header">Payment Date</f:facet>
													<h:outputText value="#{payment.receipt.date}">
														<f:convertDateTime pattern="dd MMM yyyy HH:mm" timeZone="GMT+05:30"/>
													</h:outputText>
												</rich:column>									
												<rich:column>
													<f:facet name="header">Receipt</f:facet>
													<a href="#" onclick="launchChildWindow('${request.contextPath}/pages/receipt.jsf?id=#{payment.receipt.id}')">#{payment.receipt.id}</a>
												</rich:column>
												<rich:column>
													<f:facet name="header">Amount</f:facet>
													<h:outputText value="#{payment.amount} " />
												</rich:column>
												<rich:column>
													<f:facet name="header">Is Refund</f:facet>
													#{payment.refund ? 'Yes':''}													
												</rich:column>											
											</rich:dataTable>										
										</td>
									</tr>
								</table>
							</rich:tab>
						</rich:tabPanel>			
					</td>
				</tr>
					<tr>
						<td colspan="6" style="background-color: #FFBF80; font-weight: bolder; font-size: 10pt;" align="center">
							Receipts				
						</td>
					</tr>
					<tr>
						<td colspan="6">
							<rich:dataTable value="#{paymentController.receipts}" var="aBean" 
											id="dtRcpts" style="display:#{paymentController.admission.id le 0 ? 'none':'block' }"
											rows="5">							
								<rich:column>
									<f:facet name="header">ID</f:facet>
									<a href="#" onclick="launchChildWindow('${request.contextPath}/pages/receipt.jsf?id=#{aBean.id}')">#{aBean.id}</a>
								</rich:column>
								<rich:column>
									<f:facet name="header">Date</f:facet>
									<h:outputText value="#{aBean.date}">
										<f:convertDateTime pattern="dd MMM yyyy HH:mm" timeZone="GMT+05:30"/>
									</h:outputText>
								</rich:column>
								<rich:column>
									<f:facet name="header">Current Receipt</f:facet>
									<h:selectBooleanCheckbox value="#{aBean.selected}">
										<a4j:support event='onclick' action="#{paymentController.selectReceipt}"
					   											reRender="dtRcpts,receiptInfo">
					   							</a4j:support>
									</h:selectBooleanCheckbox>									
								</rich:column>
								<f:facet name="footer">
									<rich:datascroller	for="dtRcpts"/>
								</f:facet>
							</rich:dataTable>				
						</td>
					</tr>
				</table>
		</h:form>
		
		
	<rich:modalPanel id="refundSecurityDeposit" width="450" zindex="2000" height="300" resizeable="false"  >
			<f:facet name="header">
				<h:outputText value="Refund Security Deposit"/>
			</f:facet>		
			<h:form>
				<table width="100%" align="left">					
					<tr>
						<td valign="top">
							<h:panelGrid columns="2">
								<h:outputLabel value="Amount"/>
								<h:inputText value="0"/>
							</h:panelGrid>
							
						</td>
						<td valign="top">
							<h:panelGrid columns="1">
								<a4j:commandButton oncomplete="Richfaces.hideModalPanel('refundSecurityDeposit'); return false;" value="OK" />
								<a4j:commandButton onclick="Richfaces.hideModalPanel('refundSecurityDeposit'); return false;" value="Close"/>
							</h:panelGrid>						
						</td>
					</tr>
				</table>
			</h:form>
	</rich:modalPanel>	
		
	</ui:define>
</ui:composition>